package org.spring.spring;

import org.apache.catalina.LifecycleException;
import org.apache.catalina.Server;
import org.apache.catalina.Service;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.core.StandardEngine;
import org.apache.catalina.core.StandardHost;
import org.apache.catalina.startup.Tomcat;
import org.spring.application.config.SpringConfig;
import org.spring.application.interf.AInterface;
import org.spring.application.service.*;


/**
 * @projectName: sky-springboot
 * @package: org.spring.spring
 * @className: SpringApplication
 * @author: zhl
 * @description: TODO
 * @date: 2024/3/27 15:36
 * @version: 1.0
 */
public class SpringApplication {

    public static void run(String[] args, Class<SpringApplication> springApplicationClass) {

        // 容器应用上下文加载配置类
        SkyApplicationContext skyApplicationContext = new SkyApplicationContext(SpringConfig.class);

        System.out.println("*********************************************");

        ApiService apiService = (ApiService) skyApplicationContext.getBean("apiService");
        apiService.getField();

        System.out.println("*********************************************");

        UserService userService = (UserService) skyApplicationContext.getBean("userService");
        userService.getField();

        System.out.println("*********************************************");

        OrderService orderService = (OrderService) skyApplicationContext.getBean("orderService");
        orderService.printFieldBean();

        System.out.println("*********************************************");

        AService aService = (AService) skyApplicationContext.getBean("aService");
        aService.getFieldMessage();

        BService bService = (BService) skyApplicationContext.getBean("bService");
        bService.getFieldMessage();

//        AnnotationConfigWebApplicationContext annotationConfigWebApplicationContext = new AnnotationConfigWebApplicationContext();
//        annotationConfigWebApplicationContext.register(springApplicationClass);
//        // 启动spring容器
//        annotationConfigWebApplicationContext.refresh();
//        createTomcat(annotationConfigWebApplicationContext);
    }

    private static void createTomcat(){
        Tomcat tomcat = new Tomcat();

        Server server = tomcat.getServer();
        Service service = server.findService("Tomcat");

        Connector connector = new Connector();
        connector.setPort(9090);

        StandardEngine standardEngine = new StandardEngine();
        standardEngine.setDefaultHost("localhost");

        StandardHost standardHost = new StandardHost();
        standardHost.setName("localhost");

        String contextPath = "";
        StandardContext standardContext = new StandardContext();
        standardContext.setPath(contextPath);
        standardContext.addLifecycleListener(new Tomcat.DefaultWebXmlListener());

        standardHost.addChild(standardContext);
        standardEngine.addChild(standardHost);

        service.setContainer(standardEngine);
        service.addConnector(connector);

//        tomcat.addServlet(contextPath,"dispatcher",new DispatcherServlet(webApplicationContext));
//        standardContext.addServletMappingDecoded("/","dispatcher");

        try {
            tomcat.start();
        } catch (LifecycleException e) {
            throw new RuntimeException(e);
        }
    }
}
